package tools.clustering_old;

import java.util.ArrayList;
import java.util.Iterator;

public class kMeans implements runtjoho{

	public void Runtjoho(Object c){
		ArrayList vector=((clusterVector)c).getVector();
		ArrayList seeds=((clusterVector)c).getSeeds();
		double minDistance=999999999,curDistance=0;
		int position=0,nr=0;
		for (Iterator iter = seeds.iterator(); iter.hasNext();) {
			ArrayList seed = (ArrayList) iter.next();
			curDistance=calculateDistance(seed,vector);
			if(curDistance<minDistance){
				position=nr;
				minDistance=curDistance;
			}
			nr++;
		}
		((clusterVector)c).setCluster(position);
		
	}
	private double calculateDistance(ArrayList a,ArrayList b)throws Exception{
		if(a.size()!=b.size())
			throw new Exception("Vector sizes disagree");
		double distance=0;
		for (Iterator iterA = a.iterator(),iterB=b.iterator(); iterA.hasNext()&&iterB.hasNext();) {
			Double ai = (Double) iterA.next();
			Double bi = (Double) iterB.next();
			 //loop over both vectors
			distance+=(ai.doubleValue()-bi.doubleValue())*(ai.doubleValue()-bi.doubleValue());
		}
		return Math.sqrt(distance);
	}
}
